//
//  MSGDetailViewController.m
//  HubeiHotel
//
//  Created by 张露杰 on 18/3/21.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "MSGDetailViewController.h"

@interface MSGDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, retain) NSDictionary *data;
@end

@implementation MSGDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    [self getDetail];
}

-(void)creatView
{
    [self initTitleWithTitle:@"消息详情"];
    [self.view addSubview:self.table];
}

-(UITableView *)table//创建View
{
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _table.backgroundColor = [UIColor whiteColor];
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.showsVerticalScrollIndicator = NO;
        _table.delegate = self;
        _table.dataSource = self;
        _table.bounces = NO;
    }
    return _table;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, tableView.frame.size.height)];
    
    UILabel *titleLabel = [TomamaUI createLabel:CGRectMake(15*screenW_6, 22*screenW_6, SCREENW - 30*screenW_6, [TomamaUI getStringHeightWith:_data[@"title"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont boldSystemFontOfSize:22*screenW_6] andLineSpacing:8*screenW_6]) textAligment:0 font:[UIFont boldSystemFontOfSize:22*screenW_6] textColor:TextBlack text:_data[@"title"]];
    [view addSubview:titleLabel];
    titleLabel.numberOfLines = 0;
    if (titleLabel.frame.size.height - 22*screenW_6 > 8*screenW_6) {
        [titleLabel setLineSpaceWithLineSpacing:8*screenW_6 text:_data[@"title"]];
    }
    
    UILabel *timeLabel = [TomamaUI createLabel:CGRectMake(15*screenW_6, CGRectGetMaxY(titleLabel.frame) + 15*screenW_6, SCREENW - 30*screenW_6, 14*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:14*screenW_6] textColor:TextGray text:_data[@"createDate"]];
    [view addSubview:timeLabel];
    
    UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(timeLabel.frame) + 26*screenW_6, SCREENW - 30*screenW_6, view.frame.size.height - CGRectGetMaxY(timeLabel.frame) - 36*screenW_6)];
    [view addSubview:web];
    web.scrollView.showsVerticalScrollIndicator = NO;
    web.scrollView.showsHorizontalScrollIndicator = NO;
    [web loadHTMLString:_data[@"content"] baseURL:nil];
    web.scrollView.bounces = NO;
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return tableView.frame.size.height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

-(void)getDetail
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/other/messageDetails" Dic:@{@"id":_msgId} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _data = responseDic[@"data"];
                    [_table reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
